package com.zHan.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zHan.model.dto.SysLoginLogQueryDto;
import com.zHan.model.system.SysLoginLog;
import com.zHan.system.mapper.SysLoginLogMapper;
import com.zHan.system.service.ISysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 系统访问记录 服务实现类
 * </p>
 *
 * @author zHan
 * @since 2023-06-27
 */
@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements ISysLoginLogService {

    @Autowired
    private SysLoginLogMapper loginLogMapper;

    /**
     * 记录登录日志
     *
     * @param username
     * @param status
     * @param ipaddr
     * @param message
     */
    @Override
    public void recordLoginLog(String username, Integer status, String ipaddr, String message) {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setUsername(username);
        loginLog.setStatus(status);
        loginLog.setIpaddr(ipaddr);
        loginLog.setMsg(message);
        loginLog.setAccessTime(new Date());
        loginLog.setUpdateTime(new Date());
        loginLogMapper.insert(loginLog);
    }

    /**
     * 登录日志分页条件查询
     *
     * @param page
     * @param pageSize
     * @param logQueryVo
     * @return
     */
//    @Override
    public Page<SysLoginLog> selectPageQuery(Long page, Long pageSize,
                                             SysLoginLogQueryDto logQueryVo) {
        // 创建Page对象
        Page<SysLoginLog> pageInfo = new Page<>(page, pageSize);
        // 创建条件构造器
        LambdaQueryWrapper<SysLoginLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 获得查询参数
        String username = logQueryVo.getUsername();
        String timeBegin = logQueryVo.getCreateTimeBegin();
        String timeEnd = logQueryVo.getCreateTimeEnd();

        // 查询条件
        if (!StringUtils.isEmpty(username)) {
            lambdaQueryWrapper.like(SysLoginLog::getUsername, username);
        }

        if (!StringUtils.isEmpty(timeBegin)) {
            lambdaQueryWrapper.ge(SysLoginLog::getUpdateTime, timeBegin);
        }
        if (!StringUtils.isEmpty(timeEnd)) {
            lambdaQueryWrapper.le(SysLoginLog::getUpdateTime, timeEnd);
        }

        // 执行分页方法
        page(pageInfo, lambdaQueryWrapper);

        return pageInfo;
    }


}
